package com.fashion.service.person;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import com.fashion.model.person.Person;
import com.fashion.utils.PinyinUtils;
import com.fashion.utils.StringUtils;

@Service("personService")
public class PersonService {
	@Resource(name = "hibernateTemplate")
	private HibernateTemplate template;
	
	public List<Person> find(int offset, int limit, String order){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		
		if(order != null && "".equals(order)){
				criteria.addOrder(Order.desc(order));
		}else{
			criteria.addOrder(Order.desc("priority"));
		}
		return template.findByCriteria(criteria, offset, limit);
	}
	
	public List<Person> findNotStartWithEn(int offset, int limit){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);

		List<String> list = new ArrayList();
		for(int i = 97; i<=122; i++){
			list.add(String.valueOf((char)i));
		}
		for(String ch : list){
			criteria.add(Restrictions.not(Restrictions.ilike("indexName", ch, MatchMode.START)));
		}
		criteria.addOrder(Order.desc("priority"));
		criteria.addOrder(Order.asc("id"));
		return template.findByCriteria(criteria, offset, limit);
	}
	
	public List<Person> findByFirstChar(String first, int offset, int limit){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		criteria.add(Restrictions.ilike("indexName", first, MatchMode.START));
		criteria.addOrder(Order.desc("priority"));
		criteria.addOrder(Order.asc("id"));
		return template.findByCriteria(criteria, offset, limit);
	}
	
	public long countNotStartWithEn(){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);

		List<String> list = new ArrayList();
		for(int i = 97; i<=122; i++){
			list.add(String.valueOf((char)i));
		}
		for(String ch : list){
			criteria.add(Restrictions.not(Restrictions.ilike("indexName", ch, MatchMode.START)));
		}
		criteria.setProjection(Projections.count("id"));
		List result = template.findByCriteria(criteria);
		if(result.size() == 0)
			return 0;
		return ((Integer)template.findByCriteria(criteria).get(0)).intValue();
	}
	
	public long countByFirstChar(String first){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		criteria.add(Restrictions.ilike("indexName", first, MatchMode.START));
		criteria.setProjection(Projections.count("id"));
		List result = template.findByCriteria(criteria);
		if(result.size() == 0)
			return 0;
		return ((Integer)template.findByCriteria(criteria).get(0)).intValue();
	}
	
	public List<Person> findByName(String name, int offset, int limit){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		criteria.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		criteria.addOrder(Order.desc("priority"));
		criteria.addOrder(Order.asc("id"));
		return template.findByCriteria(criteria, offset, limit);
	}
	
	public long countByName(String name){
		DetachedCriteria criteria = DetachedCriteria.forClass(Person.class);
		criteria.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
		criteria.setProjection(Projections.count("id"));
		List result = template.findByCriteria(criteria);
		if(result.size() == 0)
			return 0;
		return ((Integer)template.findByCriteria(criteria).get(0)).intValue();
	}
	
	public Person findById(String id){
		return template.get(Person.class, id);
		
	}
	
	public List<Person> findByName(String enTitle){
		return (List<Person>)template.find("from Person where name=?", new String[]{enTitle});
	}
	
	public void add(Person person){		
		person.setId(StringUtils.createTimeStamp());
		person.setIndexName(PinyinUtils.getPinyinIndex(person.getName()));
		template.save(person);
	}
	
	public void delete(Person person){
		template.delete(person);
	}
	
	public void update(Person person){
		template.update(person);
	}
}
